package com.zy.config.common;

import com.zy.common.core.constant.CharacterCon;
import com.zy.config.feign.PropertyFeign;
import com.zy.config.utils.RemoteArgs;
import com.zy.config.utils.RemotePropertySource;
import com.zy.db.odbc.OdbcConnectParam;
import com.zy.db.odbc.OdbcUtil;
import com.zy.db.utils.DataSourceUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Resource
    private PropertyFeign propertyFeign;

    public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "remoteConfiguration";

    @Value("${spring.application.name}")
    private String appId;

    private final String CONFIG = "config";

    //Properties对象
    private Properties properties = new Properties();

    @SneakyThrows
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        org.springframework.core.io.Resource resource = new ClassPathResource("application.yml");
        properties.load(resource.getInputStream());
        String appId = properties.getProperty("name");
        if (!appId.equals(CONFIG)) {
            properties = new Properties();
            //循环添加
            //从classpath路径下面查找文件
            //  String str = propertyFeign.propertyToString(appId);
            properties.load(new StringReader(selectMaster(appId)));
            if (Optional.ofNullable(properties).isPresent()) {
                RemoteArgs remoteArgs = new RemoteArgs(properties);
                RemotePropertySource remotePropertySource = new RemotePropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME, remoteArgs);
                // 更新Environment
                environment.getPropertySources().addLast(remotePropertySource);
            }
        }

    }

    public String selectMaster(String name ) throws SQLException {
        String sql = " select t.* " +
                " from app_config_details t " +
                "         left join app_config t1 on t.app_config_id = t1.id " +
                " where t1.app_id = '"+name+"' " +
                "  and t1.env = 'dev' ";
        StringBuffer sb = new StringBuffer();
        Connection ct = OdbcUtil.connect(new OdbcConnectParam().configConnect());
        Statement sm = null;
        ResultSet rs = null;
        sm = ct.createStatement();
        rs = sm.executeQuery(sql);
        OdbcUtil.toMap(rs).forEach(e -> sb.append(e.get("acd_key") + CharacterCon.COLON + e.get("acd_value") + "\n"));
        return sb.toString();
    }
}
